import 'package:flutter/material.dart';
import 'package:znmm/UI/utils/my_global.dart';
import 'package:webview_flutter/webview_flutter.dart';

import 'dialogs/cn_custom_dialog.dart';

class CustomWebView extends StatefulWidget {
  final String initialUrl;
  final String titleStr;
  final bool bIsShowTips;

  const CustomWebView({
    Key? key,
    required this.initialUrl,
    this.titleStr = " ",
    this.bIsShowTips = false,
  }) : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  _CustomWebViewState createState() => _CustomWebViewState();
}

class _CustomWebViewState extends State<CustomWebView> {
  bool _isLoading = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: MyGlobal.getNavigationBar(context, widget.titleStr),
      body: Padding(
        padding: const EdgeInsets.fromLTRB(10, 0, 10, 15),
        child: Stack(
          children: [
            WebView(
              initialUrl: widget.initialUrl,
              onPageFinished: (_) {
                if (mounted) {
                  setState(() {
                    _isLoading = false;
                    _showTips();
                  });
                }
              },
              onWebViewCreated: (WebViewController webViewController) {},
              javascriptMode: JavascriptMode.unrestricted,
            ),
            if (_isLoading)
              const Center(
                //child: CircularProgressIndicator(),
                child: Text("Loading..."),
              ),
          ],
        ),
      ),
    );
  }

  void _showTips() {
    showDialog(
      context: context,
      builder: (context) => const CustomDialog(
        title: "Tips",
        content: 'Step:Click Center->Sign In->Click Cart.',
        confirmText: "Ok",
        bIsShowCancelBtn: false,
        bIsShowOkBtn: false,
      ),
    ).then((value) {
      if (value != null && value) {}
    });
  }
}
